package com.leostadyn.stackfarm.timestatistics.net.base;

import com.leostadyn.stackfarm.timestatistics.net.interceptor.HeaderInterceptor;
import com.leostadyn.stackfarm.timestatistics.net.interceptor.TokenInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class HttpService {

    //定义时间超时！
    public static final int WRITETIMEOUT = 60_000;
    public static final int FILETETIMEOUT = 120_000;
    public static final int READTIMEOUT = 60_000;
    public static final int CONNECTTIMEOUT = 60_000;
    // OkHttpClient 要保持单例，防止线程池太多线程！
    //  OkHttp performs best when you create a single {@code OkHttpClient} instance and reuse it for all of your HTTP calls.
    private volatile static HttpService mHttpService;
    private Retrofit mRetrofit;
    private OkHttpClient mClient;

    //单列
    public static HttpService initialize() {
        if (mHttpService == null) {
            return new HttpService();
        }
        // 提前初始化，加快请求
//        checkHttpClient();
//        getIintretrofit();
        return mHttpService;
    }

    public <T> T create(final Class<T> service) {
        if (mRetrofit != null) {
            return mRetrofit.create(service);
        }
        checkHttpClient();
        mRetrofit = new Retrofit
                .Builder()
                //韩运畅
//                .baseUrl("http://10.0.18.98:8080/")
                //外网映射
                .baseUrl("http://mgtserver.hwelltech.net:88/")
                //徐中元
//                .baseUrl("http://10.0.18.146:8080/")
                .client(mClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // 使用RxJava作为回调适配器
                .addConverterFactory(new StringConverterFactory()) // 使用Gson作为数据转换器
                .addConverterFactory(GsonConverterFactory.create()) // 使用Gson作为数据转换器
                .build();
        return mRetrofit.create(service);
    }

    public OkHttpClient getClient() {
        checkHttpClient();
        return mClient;
    }

    // 初始化 mClient ，让他变成唯一
    private void checkHttpClient() {
        if (mClient != null) {
            return;
        }
        OkHttpClient.Builder client = new OkHttpClient.Builder();


        client.retryOnConnectionFailure(false);
        client.writeTimeout(WRITETIMEOUT, TimeUnit.SECONDS);
        client.readTimeout(READTIMEOUT, TimeUnit.SECONDS);
        client.connectTimeout(CONNECTTIMEOUT, TimeUnit.SECONDS);

        client.addInterceptor(new TokenInterceptor());
        client.addInterceptor(new HeaderInterceptor());

        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        client.addInterceptor(loggingInterceptor);
        mClient = client.build();
    }


    public void readTimeoutMillis(int mTime) {
        checkHttpClient();
        if (mClient.readTimeoutMillis() != mTime) {
            mClient.newBuilder().readTimeout(mTime, TimeUnit.SECONDS);
        }
    }

    public void writeTimeout(int mTime) {
        checkHttpClient();
        if (mClient.writeTimeoutMillis() != mTime) {
            mClient.newBuilder().writeTimeout(mTime, TimeUnit.SECONDS);
        }

    }

    public void connectTimeout(int mTime) {
        checkHttpClient();
        if (mClient.connectTimeoutMillis() != mTime) {
            mClient.newBuilder().connectTimeout(mTime, TimeUnit.SECONDS);
        }
    }

    //检测当前设置的Timeout时间是不是合理！不合理就重置，保持唯一的mClient连接池。
    public void timeOut(int mTime) {
        checkHttpClient();
        if (mClient.connectTimeoutMillis() != mTime) {
            mClient.newBuilder().readTimeout(mTime, TimeUnit.SECONDS).writeTimeout(mTime, TimeUnit.SECONDS).connectTimeout(mTime, TimeUnit.SECONDS);
        }
    }
}
